using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using EHSubmit.Model;
using EHSubmit.Util;

namespace EHSubmit.Logic
{
    /// <summary>
    ///HomeworkManager 的摘要说明
    /// </summary>
    public class HomeworkManager             // ..p
    {
        private const string SQL_SELECT_Homework = "SELECT WID,GoalID,GoalName,StudentID,AttachPath,Description,SubmitTime,UpdateTime,SubYear,Term,IsDeleted,Scores,Comment,CommentTime,IsViewed,GoalType FROM Homework WHERE WID=@WID";
        private const string SQL_SELECT_Student_Homeworks = "SELECT WID,GoalID,GoalName,StudentID,AttachPath,Description,SubmitTime,UpdateTime,SubYear,Term,IsDeleted,Scores,Comment,CommentTime,IsViewed,GoalType FROM Homework WHERE StudentID=@StudentID ORDER BY UpdateTime DESC";
        private const string SQL_INSERT_Homework = "INSERT INTO Homework(GoalID,GoalName,StudentID,AttachPath,Description,SubmitTime,UpdateTime,SubYear,Term,IsDeleted,Scores,Comment,CommentTime,IsViewed,GoalType) VALUES (@GoalID,@GoalName,@StudentID,@AttachPath,@Description,@SubmitTime,@UpdateTime,@SubYear,@Term,@IsDeleted,@Scores,@Comment,@CommentTime,@IsViewed,@GoalType)";
        private const string SQL_DELETE_FIRST = "UPDATE Homework SET IsDeleted = 1 WHERE WID = @WID";
        private const string SQL_DELETE_COMPELETELY = "DELETE FROM Homework WHERE WID = @WID";
        private const string SQL_UPDATE_Homework = "UPDATE Homework SET GoalID=@GoalID,GoalName=@GoalName,StudentID=@StudentID,AttachPath=@AttachPath,Description=@Description,SubmitTime=@SubmitTime,UpdateTime=@UpdateTime,SubYear=@SubYear,Term=@Term,IsDeleted=@IsDeleted,Scores=@Scores,Comment=@Comment,CommentTime=@CommentTime,IsViewed=@IsViewed WHERE WID=@WID";
        private const string PARM_WID = "@WID";
        private const string PARM_GoalID = "@GoalID";
        private const string PARM_GoalName = "@GoalName";
        private const string PARM_StudentID = "@StudentID";
        private const string PARM_AttachPath = "@AttachPath";
        private const string PARM_Description = "@Description";
        private const string PARM_SubmitTime = "@SubmitTime";
        private const string PARM_UpdateTime = "@UpdateTime";
        private const string PARM_SubYear = "@SubYear";
        private const string PARM_Term = "@Term";
        private const string PARM_IsDeleted = "@IsDeleted";
        private const string PARM_Scores = "@Scores";
        private const string PARM_Comment = "@Comment";
        private const string PARM_CommentTime = "@CommentTime";
        private const string PARM_IsViewed = "@IsViewed";
        private const string PARM_GoalType = "@GoalType";

        /// <summary>
        /// 构造函数
        /// </summary>
        public HomeworkManager() { }


        /// <summary>
        /// 获取提交的作业信息，找不到时返回null
        /// </summary>
        /// <param name="hid">作业ID</param>
        /// <returns>作业信息实例</returns>
        public Homework GetHomework(int wid)
        {
            try
            {

                SqlParameter parm = new SqlParameter(PARM_WID, SqlDbType.Int);
                parm.Value = Convert.ToInt32(wid);                                    //作业ID
                SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_Homework, parm);
                
                //homework.GoalID = (int)homeworkDate["GoalID"];                                //作业所属Goal的ID                       
                //homework.StudentID = (string)homeworkDate["StudentID"];                       //上交作业的学生的ID
                //homework.AttachPath = (string)homeworkDate["AttachPath"];                     //作业路径
                //homework.Description = (string)homeworkDate["Description"];                   //作业描述
                //homework.SubmitTime = (DateTime)homeworkDate["SubmitTime"];                   //提交时间
                //homework.UpdateTime = (DateTime)homeworkDate["UpdateTime"];                   //最后更新时间
                //homework.SubYear = (string)homeworkDate["SubYear"];                           //作业所属学年
                //homework.Term = (string)homeworkDate["Term"];                                 //作业所属学期
                //homework.IsDeleted = (int)homeworkDate["IsDeleted"];                          //是否被删除
                //homework.Scores = (int)homeworkDate["Scores"];                                //老师给的作业分数
                //homework.Comment = (string)homeworkDate["Comment"];                           //老师评价
                //homework.CommentTime = (DateTime)homeworkDate["CommentTime"];                 //评价时间
                //homework.IsViewed = (int)homeworkDate["IsViewed"];                            //是否被老师看过
                if (rdr.Read())
                {
                    Homework work = new Homework();

                    work.ID = (Int32)rdr["WID"];
                    work.GoalID = (Int32)rdr["GoalID"];
                    work.GoalName = rdr["GoalName"] is DBNull ? string.Empty : (string)rdr["GoalName"];
                    work.Description = rdr["Description"] is DBNull ? string.Empty : (string)rdr["Description"];
                    work.AttachPath = rdr["AttachPath"] is DBNull ? string.Empty : (string)rdr["AttachPath"];
                    work.Comment = rdr["Comment"] is DBNull ? string.Empty : (string)rdr["Comment"];
                    work.SubmitTime = rdr["SubmitTime"] is DBNull ? DateTime.Now : (DateTime)rdr["SubmitTime"];
                    work.CommentTime = rdr["CommentTime"] is DBNull ? DateTime.Now : (DateTime)rdr["CommentTime"];
                    work.IsDeleted = 0;             //.. 待处理
                    work.IsViewed = 0;              //.. 待处理
                    work.Scores = rdr["Scores"] is DBNull ? -1 : (Int32)rdr["Scores"];
                    work.StudentID = rdr["StudentID"] is DBNull ? string.Empty : (string)rdr["StudentID"];
                    work.SubYear = rdr["SubYear"] is DBNull ? string.Empty : (string)rdr["SubYear"];
                    work.Term = rdr["Term"] is DBNull ? "1" : (string)rdr["Term"];
                    work.UpdateTime = rdr["UpdateTime"] is DBNull ? DateTime.Now : (DateTime)rdr["UpdateTime"];
                    work.GoalType = (Int32)rdr["GoalType"];

                    return work;
                }
                else // 没找到，返回空
                {
                    return null;
                }

                
            }
            catch(Exception e)
            {
                LogHelper.Log(e.Message, e);

                throw e;
            }
           
        }




        /// <summary>
        /// 上交的作业信息被添加
        /// </summary>
        /// <param name="Homework">作业信息</param>
        public void AddHomework(EHSubmit.Model.Homework Homework)
        {
            int i; 
            SqlParameter[] parms = new SqlParameter[15];
            parms[0] = new SqlParameter(PARM_GoalID, SqlDbType.Int);                         //作业所属的Goal的ID
            parms[1] = new SqlParameter(PARM_GoalName, SqlDbType.NVarChar, 100);             //作业所属的Goal的Title
            parms[2] = new SqlParameter(PARM_StudentID, SqlDbType.NChar, 13);                //上交学生的ID
            parms[3] = new SqlParameter(PARM_AttachPath, SqlDbType.NVarChar, 100);           //作业路径
            parms[4] = new SqlParameter(PARM_Description, SqlDbType.NChar, 200);             //作业描述
            parms[5] = new SqlParameter(PARM_SubmitTime, SqlDbType.DateTime);                //上交时间
            parms[6] = new SqlParameter(PARM_UpdateTime, SqlDbType.DateTime);                //作业最后更新时间
            parms[7] = new SqlParameter(PARM_SubYear, SqlDbType.NChar, 9);                   //所属学年
            parms[8] = new SqlParameter(PARM_Term, SqlDbType.NChar, 1);                      //所属学期
            parms[9] = new SqlParameter(PARM_IsDeleted, SqlDbType.Int);                      //是否被删除
            parms[10] = new SqlParameter(PARM_Scores, SqlDbType.Int);                        //老师给的作业分数
            parms[11] = new SqlParameter(PARM_Comment, SqlDbType.NVarChar,200);              //老师给的作业评价
            parms[12] = new SqlParameter(PARM_CommentTime, SqlDbType.DateTime);              //评价时间
            parms[13] = new SqlParameter(PARM_IsViewed, SqlDbType.Int);                      //是否被老师看过
            parms[14] = new SqlParameter(PARM_GoalType, SqlDbType.Int);                      //作业类型
         
            parms[0].Value = Homework.GoalID;
            parms[1].Value = Homework.GoalName;
            parms[2].Value = Homework.StudentID;
            parms[3].Value = Homework.AttachPath;
            parms[4].Value = Homework.Description;
            parms[5].Value = Convert.ToDateTime(Homework.SubmitTime);
            parms[6].Value = Convert.ToDateTime(Homework.UpdateTime);                        
            parms[7].Value = Homework.SubYear;
            parms[8].Value = Homework.Term;
            parms[9].Value = Homework.IsDeleted;
            parms[10].Value = Homework.Scores;
            parms[11].Value = Homework.Comment;
            parms[12].Value = Convert.ToDateTime(Homework.CommentTime);
            parms[13].Value = Homework.IsViewed;
            parms[14].Value = Homework.GoalType;

            i = (int)SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_INSERT_Homework, parms);
        }



        /// <summary>
        /// 作业信息被彻底删除
        /// </summary>
        /// <param name="homework">作业信息对象</param>
        public void CompletelyDeleteHomework(Homework homework)
        {
            try
            {
                if (homework == null)
                {
                    throw new Exception("作业信息ID不能为空");
                }
                else
                {
                    SqlParameter parm = new SqlParameter(PARM_WID, SqlDbType.Int);                     //作业ID
                    int nResult;
                    parm.Value = Convert.ToInt32(homework.ID);
                    nResult = (int)SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_DELETE_COMPELETELY, parm);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Log(ex.Message, ex);
                throw ex;
            }
        }

       
        /// <summary>
        /// 更新作业
        /// </summary>
        /// <param name="homewor">提交的作业对象</param>
        public void UpdateHomework(EHSubmit.Model.Homework Homework)
        {
            try
            {
                if (Homework == null)
                {
                    throw new Exception("作业ID不能为空");
                }
                else
                {
                    int result;
                    SqlParameter[] parms = new SqlParameter[15];
                    parms[0] = new SqlParameter(PARM_GoalID, SqlDbType.Int);
                    parms[1] = new SqlParameter(PARM_GoalName, SqlDbType.NVarChar, 100);
                    parms[2] = new SqlParameter(PARM_StudentID, SqlDbType.NChar, 13);                      //学生ID
                    parms[3] = new SqlParameter(PARM_AttachPath, SqlDbType.NVarChar, 100);
                    parms[4] = new SqlParameter(PARM_Description, SqlDbType.NChar, 200);
                    parms[5] = new SqlParameter(PARM_SubmitTime, SqlDbType.DateTime);
                    parms[6] = new SqlParameter(PARM_UpdateTime, SqlDbType.DateTime);
                    parms[7] = new SqlParameter(PARM_SubYear, SqlDbType.NChar, 9);
                    parms[8] = new SqlParameter(PARM_Term, SqlDbType.NChar, 1);
                    parms[9] = new SqlParameter(PARM_IsDeleted, SqlDbType.Int);
                    parms[10] = new SqlParameter(PARM_Scores, SqlDbType.Int);
                    parms[11] = new SqlParameter(PARM_Comment, SqlDbType.NVarChar, 200);
                    parms[12] = new SqlParameter(PARM_CommentTime, SqlDbType.DateTime);
                    parms[13] = new SqlParameter(PARM_IsViewed, SqlDbType.Int);
                    parms[14] = new SqlParameter(PARM_WID, SqlDbType.Int);
                    parms[0].Value = Homework.GoalID;
                    parms[1].Value = Homework.GoalName;
                    parms[2].Value = Homework.StudentID;
                    parms[3].Value = Homework.AttachPath;
                    parms[4].Value = Homework.Description;
                    parms[5].Value = Convert.ToDateTime(Homework.SubmitTime);
                    parms[6].Value = Convert.ToDateTime(Homework.UpdateTime);
                    parms[7].Value = Homework.SubYear;
                    parms[8].Value = Homework.Term;
                    parms[9].Value = Homework.IsDeleted;
                    parms[10].Value = Homework.Scores;
                    parms[11].Value = Homework.Comment;
                    parms[12].Value = Convert.ToDateTime(Homework.CommentTime);
                    parms[13].Value = Homework.IsViewed;
                    parms[14].Value = Homework.ID;

                    result = (int)SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_UPDATE_Homework, parms);

                }
            }
            catch(Exception e)
            {
                LogHelper.Log(e.Message, e);
                throw e;
            }
            
            
        }

        /// <summary>
        /// 获得以学生ID指定的所有作业集合
        /// </summary>
        /// <param name="sid">学生ID</param>
        /// <returns>作业集合</returns>
        public IList<Homework> ListFromStudent(string sid)
        {
            IList<Homework> works = new List<Homework>();

            SqlParameter parm = new SqlParameter(PARM_StudentID, SqlDbType.NVarChar,13);
            parm.Value = sid;   // 学生ID


            using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_Student_Homeworks, parm))
            {
                // SELECT WID,GoalID,GoalName,StudentID,AttachPath,Description,SubmitTime,UpdateTime,SubYear,Term,IsDeleted,Scores,Comment,CommentTime,IsViewed FROM Homework WHERE StudentID=@StudentID ORDER BY UpdateTime DESC
                //homework.GoalID = (int)homeworkDate["GoalID"];                                //作业所属Goal的ID                       
                //homework.StudentID = (string)homeworkDate["StudentID"];                       //上交作业的学生的ID
                //homework.AttachPath = (string)homeworkDate["AttachPath"];                     //作业路径
                //homework.Description = (string)homeworkDate["Description"];                   //作业描述
                //homework.SubmitTime = (DateTime)homeworkDate["SubmitTime"];                   //提交时间
                //homework.UpdateTime = (DateTime)homeworkDate["UpdateTime"];                   //最后更新时间
                //homework.SubYear = (string)homeworkDate["SubYear"];                           //作业所属学年
                //homework.Term = (string)homeworkDate["Term"];                                 //作业所属学期
                //homework.IsDeleted = (int)homeworkDate["IsDeleted"];                          //是否被删除
                //homework.Scores = (int)homeworkDate["Scores"];                                //老师给的作业分数
                //homework.Comment = (string)homeworkDate["Comment"];                           //老师评价
                //homework.CommentTime = (DateTime)homeworkDate["CommentTime"];                 //评价时间
                //homework.IsViewed = (int)homeworkDate["IsViewed"];                            //是否被老师看过
                while (rdr.Read())
                {
                    Homework work = new Homework();

                    work.ID = (Int32)rdr["WID"];
                    work.GoalID = (Int32)rdr["GoalID"];
                    work.StudentID = rdr["StudentID"] is DBNull ? string.Empty : (string)rdr["StudentID"];
                    work.GoalName = rdr["GoalName"] is DBNull ? string.Empty : (string)rdr["GoalName"];
                    work.Description = rdr["Description"] is DBNull ? string.Empty : (string)rdr["Description"];
                    work.AttachPath = rdr["AttachPath"] is DBNull ? string.Empty : (string)rdr["AttachPath"];
                    work.Comment = rdr["Comment"] is DBNull ? string.Empty : (string)rdr["Comment"];
                    work.SubmitTime = rdr["SubmitTime"] is DBNull ? DateTime.Now : (DateTime)rdr["SubmitTime"];
                    work.CommentTime = rdr["CommentTime"] is DBNull ? DateTime.Now : (DateTime)rdr["CommentTime"];
                    work.IsDeleted = 0;             //.. 待处理
                    work.IsViewed = 0;              //.. 待处理
                    work.Scores = rdr["Scores"] is DBNull ? -1 : (Int32)rdr["Scores"];
                    work.SubYear = rdr["SubYear"] is DBNull ? string.Empty : (string)rdr["SubYear"];
                    work.Term = rdr["Term"] is DBNull ? "1" : (string)rdr["Term"];
                    work.UpdateTime = rdr["UpdateTime"] is DBNull ? DateTime.Now : (DateTime)rdr["UpdateTime"];
                    work.GoalType = (Int32)rdr["GoalType"];

                    works.Add(work);
                }
            }
   

            return works;
        }

        /// <summary>
        /// 获得学生所提交作业的分页数据（按最后修改逆序）
        /// </summary>
        /// <param name="SID">学生ID</param>
        /// <param name="page">页号</param>
        /// <param name="size">每页大小</param>
        /// <returns>单页作业集合</returns>
        public IList<Homework> ListByPageFromStudent(string sid, int page, int size)
        {
            IList<Homework> works = new List<Homework>();

            PagedDataProvider provider = new PagedDataProvider();

            provider.TbName = "Homework";
            provider.FieldsToReturn = "*";
            provider.DoCount = false;
            provider.PageIndex = page;
            provider.PageSize = size;             // 一面十条数据
            provider.Criteria = "StudentID = " + sid;
            provider.FieldToOrder = "UpdateTime";
            provider.IsDesc = true;
            
            SqlDataReader rdr = provider.GetResults();

            try
            {
                while (rdr.Read())
                {
                    Homework work = new Homework();

                    work.ID = (Int32)rdr["WID"];
                    work.GoalID = (Int32)rdr["GoalID"];
                    work.GoalName = rdr["GoalName"] is DBNull ? string.Empty : (string)rdr["GoalName"];
                    work.Description = rdr["Description"] is DBNull ? string.Empty : (string)rdr["Description"];
                    work.AttachPath = rdr["AttachPath"] is DBNull ? string.Empty : (string)rdr["AttachPath"];
                    work.Comment = rdr["Comment"] is DBNull ? string.Empty : (string)rdr["Comment"];
                    work.SubmitTime = rdr["SubmitTime"] is DBNull ? DateTime.Now : (DateTime)rdr["SubmitTime"];
                    work.CommentTime = rdr["CommentTime"] is DBNull ? DateTime.Now : (DateTime)rdr["CommentTime"];
                    work.IsDeleted = 0;             //.. 待处理
                    work.IsViewed = 0;              //.. 待处理
                    work.Scores = rdr["Scores"] is DBNull ? -1 : (Int32)rdr["Scores"];
                    work.StudentID = rdr["StudentID"] is DBNull ? string.Empty : (string)rdr["StudentID"];
                    work.SubYear = rdr["SubYear"] is DBNull ? string.Empty : (string)rdr["SubYear"];
                    work.Term = rdr["Term"] is DBNull ? string.Empty : (string)rdr["Term"];
                    work.UpdateTime = rdr["UpdateTime"] is DBNull ? DateTime.Now : (DateTime)rdr["UpdateTime"];
                    work.GoalType = (Int32)rdr["GoalType"];

                    works.Add(work);
                }
            }
            catch (Exception e)
            {
                LogHelper.Log(e.Message, e);
                throw e;
            }
            finally
            {
                if (rdr != null)   //关闭与数据库连接
                    rdr.Close();
            }
            return works;

        }

        /// <summary>
        /// 获得某学生所有作业的总记录数
        /// </summary>
        /// <param name="sid">学生ID</param>
        /// <returns>总作业数</returns>
        public int GetCountFromStudent(string sid)
        {
            PagedDataProvider provider = new PagedDataProvider();
            provider.TbName = "Homework";
            provider.DoCount = true;
            provider.Criteria = "StudentID = " + sid;

            return provider.GetRecordCount();
        }

        /// <summary>
        /// 针对某个作业通知，获取某老师要批改的(或已经批改的)已交的作业
        /// </summary>
        /// <param name="gid">作业通知的id</param>
        /// <param name="page">页号</param>
        /// <param name="size">每页大小</param>
        /// <returns>单页的作业记录或者某个具体的作业</returns>
        public IList<Homework> ListByPageFromTeacher(string gid, int page, int size,bool isMarking)
        {
            IList<Homework> works = new List<Homework>();

            PagedDataProvider provider = new PagedDataProvider();

            provider.TbName = "Homework";
            provider.FieldsToReturn = "*";
            provider.DoCount = false;
            provider.PageIndex = page;
            provider.PageSize = size;             // 默认一面十条数据
            if (isMarking)
            {
                provider.Criteria = "GoalID = " + gid + " AND Scores = -1";
            }
            else
            {
                provider.Criteria = "GoalID = " + gid + " AND Scores != -1";
            }
            provider.FieldToOrder = "UpdateTime";
            provider.IsDesc = false;

            SqlDataReader rdr = provider.GetResults();

            try
            {
                while (rdr.Read())
                {
                    Homework work = new Homework();

                    work.ID = (Int32)rdr["WID"];
                    work.GoalID = (Int32)rdr["GoalID"];
                    work.GoalName = rdr["GoalName"] is DBNull ? string.Empty : (string)rdr["GoalName"];
                    work.Description = rdr["Description"] is DBNull ? string.Empty : (string)rdr["Description"];
                    work.AttachPath = rdr["AttachPath"] is DBNull ? string.Empty : (string)rdr["AttachPath"];
                    work.Comment = rdr["Comment"] is DBNull ? string.Empty : (string)rdr["Comment"];
                    work.SubmitTime = rdr["SubmitTime"] is DBNull ? DateTime.Now : (DateTime)rdr["SubmitTime"];
                    work.CommentTime = rdr["CommentTime"] is DBNull ? DateTime.Now : (DateTime)rdr["CommentTime"];
                    work.IsDeleted = Convert.ToByte(rdr["IsDeleted"]) == 0 ? 0 : 1;
                    work.IsViewed = Convert.ToByte(rdr["IsViewed"]) == 0 ? 0 : 1;
                    work.Scores = rdr["Scores"] is DBNull ? -1 : (Int32)rdr["Scores"];
                    work.StudentID = rdr["StudentID"] is DBNull ? string.Empty : (string)rdr["StudentID"];
                    work.SubYear = rdr["SubYear"] is DBNull ? string.Empty : (string)rdr["SubYear"];
                    work.Term = rdr["Term"] is DBNull ? string.Empty : (string)rdr["Term"];
                    work.UpdateTime = rdr["UpdateTime"] is DBNull ? DateTime.Now : (DateTime)rdr["UpdateTime"];
                    work.GoalType = (Int32)rdr["GoalType"];

                    works.Add(work);
                }
            }
            catch (Exception e)
            {
                LogHelper.Log(e.Message, e);
                throw e;
            }
            finally
            {
                if (rdr != null)   //关闭与数据库连接
                    rdr.Close();
            }
            return works;

        }

        /// <summary>
        /// 针对某个作业通知，获取某老师要批改的(或已经批改的)已交的作业数量
        /// </summary>
        /// <param name="gid">老师发布的作业通知的id</param>
        /// <returns>已交的作业记录总数</returns>
        public int GetCountFromTeacher(string gid, bool isMarking)
        {
            PagedDataProvider provider = new PagedDataProvider();
            provider.TbName = "Homework";
            provider.DoCount = true;
            if (isMarking)
            {
                provider.Criteria = "GoalID = " + gid + " AND Scores = -1";
            }
            else
            {
                provider.Criteria = "GoalID = " + gid + " AND Scores != -1";
            }
          
            return provider.GetRecordCount();
        }

        /// <summary>
        /// 针对某个作业通知,获取已交的作业数量
        /// </summary>
        /// <param name="gid"></param>
        /// <returns></returns>
        public int GetCountFromTeacher(string gid)
        {
            PagedDataProvider provider = new PagedDataProvider();
            provider.TbName = "Homework";
            provider.DoCount = true;
            provider.Criteria = "GoalID = " + gid;

            return provider.GetRecordCount();
        }
        /// <summary>
        /// 获得某个作业下的所有已提交作业(已经评分的)(暂时只填充分数)
        /// </summary>
        /// <param name="gid"></param>
        /// <returns></returns>
        public IList<Homework> ListByGoalId(int gid)
        {
            string sql = "Select scores From homework Where GoalId=@GoalId and scores != -1";

            SqlParameter[] parms = new SqlParameter[1];

            parms[0] = new SqlParameter(PARM_GoalID, SqlDbType.Int);
            parms[0].Value = gid;

            IList<Homework> list = new List<Homework>();

            using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, sql, parms))
            {
                while (rdr.Read())
                {
                    Homework work = new Homework();
                    work.Scores = (Int32)rdr["Scores"];

                    list.Add(work);
                }
            }

            return list;
        }


        /// <summary>
        /// 获取教师未批改作业
        /// </summary>
        /// <param name="tid">教师ID</param>
        public int UnViewedHomeworkNum(string tid)
        {
            try
            {
                string sql = "Select count(*) as Num from Homework,Goal Where Goal.TeacherID=@TID and Homework.GoalID = Goal.GID and Homework.Scores = -1";
                string PARM_TID = "@TID";
                SqlParameter[] parms = new SqlParameter[1];
                int Num = 0;
                parms[0] = new SqlParameter(PARM_TID, SqlDbType.Int);
                parms[0].Value = tid;
                using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, sql, parms))
                {
                    while (rdr.Read())
                    {
                        Num = (Int32)rdr["Num"];
                    }
                }
                return Num;
            }
            catch
            {
                return 0;
            }
        }

    }


}